package main

/*
给你一棵所有节点为非负值的二叉搜索树，请你计算树中任意两节点的差的绝对值的最小值。
*/
import (
	"fmt"
	"leetcode/tree/common"
	"math"
	"strings"
)

func getMinimumDifference(root *common.TreeNode) int {
	var minDiff int = math.MaxInt64
	var preNode int = -1
	walk(root, &preNode, &minDiff) // 二叉搜索树的中缀遍历就是从小到大排序的列表
	return minDiff
}

func walk(node *common.TreeNode, preNode *int, minDiff *int) {
	if node == nil {
		return
	}
	walk(node.Left, preNode, minDiff)
	if (*preNode) != -1 {
		tmp := (*preNode) - node.Val
		if tmp < 0 {
			tmp *= -1
		}
		if (*minDiff) > tmp {
			(*minDiff) = tmp
		}
	}
	(*preNode) = node.Val
	walk(node.Right, preNode, minDiff)
}

func main() {
	arr := strings.Split("236,104,701,null,227,null,911", ",")
	root := common.DeSerializeBFS(arr)
	common.MidWalk(root)
	fmt.Println()
	fmt.Println(getMinimumDifference(root))

}
